package com.zhentao.controller;

import com.alibaba.fastjson2.JSON;
import com.zhentao.pojo.ZTTest;
import lombok.SneakyThrows;
import org.HdrHistogram.AtomicHistogram;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2024/6/4 9:40
 * @Author xdm
 **/
@RestController
public class EsController {

    @Autowired
    private RestHighLevelClient client;

    @SneakyThrows
    @RequestMapping("/createDoc")
    public String createDoc(String name) {
        ZTTest ztTest = new ZTTest();
        ztTest.setName(name);
        ztTest.setAge(11);
        ztTest.setSex(1);

        IndexRequest indexRequest =
                new IndexRequest().index("zt_test3").id("123").
                        source(JSON.toJSONString(ztTest), XContentType.JSON);

        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(response);


        return "createDoc";
    }

    @SneakyThrows
    @RequestMapping("/getDoc")
    public String getDoc() {
        GetRequest getRequest = new GetRequest("zt_test", "001");
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(response);
        return response.getSourceAsString();

    }

    @SneakyThrows
    @RequestMapping("/deleteDoc")
    public String deleteDoc() {
        DeleteRequest deleteRequest = new DeleteRequest("zt_test", "001");
        DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(response);
        return "deleteDoc";
    }

    @SneakyThrows
    @RequestMapping("/search")
    public String search() {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        QueryBuilder queryBuilder = QueryBuilders.boolQuery()

                .must(QueryBuilders.termQuery("name", "小"))
                .must(QueryBuilders.termQuery("sex", "1"));

        searchSourceBuilder.query(queryBuilder);

            //new SearchRequest  不写参数的时候  是查询的所有index的
        SearchRequest searchRequest = new SearchRequest("zt_test");
        searchRequest.source(searchSourceBuilder);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(response);

        SearchHits hits=response.getHits();
        for (SearchHit hit: hits) {
            System.out.println(hit.getSourceAsString());
        }

        return "search";

    }
    @SneakyThrows
    @RequestMapping("/bulk")
    public String bulk(){

        ZTTest ztTest = new ZTTest();
        ztTest.setName("x");
        ztTest.setAge(11);
        ztTest.setSex(1);

        ZTTest ztTest2 = new ZTTest();
        ztTest2.setName("y");
        ztTest2.setAge(11);
        ztTest2.setSex(1);

        //--集合
        List<ZTTest> ztTestList=new ArrayList<>();
        ztTestList.add(ztTest);
        ztTestList.add(ztTest2);

        BulkRequest bulkRequest=new BulkRequest("zt_test");

        for (ZTTest test:ztTestList) {
            IndexRequest indexRequest
                    =new IndexRequest().source(JSON.toJSONString(test),XContentType.JSON);
            bulkRequest.add(indexRequest);
        }

        client.bulk(bulkRequest,RequestOptions.DEFAULT);
        return "bulk";

    }



}
